package cn.slipi.provider.controller;

import cn.hutool.core.lang.Assert;
import cn.slipi.common.annotation.UsedTime;
import cn.slipi.common.enums.ResponseCode;
import cn.slipi.common.exception.BizException;
import cn.slipi.common.response.Response;
import cn.slipi.common.utils.dozer.impl.ConvertBean;
import cn.slipi.provider.controller.form.test.TestForm;
import cn.slipi.provider.controller.vo.test.CopyVo;
import cn.slipi.provider.controller.vo.test.TestVo;
import cn.slipi.service.entity.TStd;
import cn.slipi.service.service.TStdService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private ConvertBean convertBean;

    @Autowired
    private TStdService stdService;

    @GetMapping("/get")
    public Response getTest() {
        TestVo testVo = new TestVo();
        testVo.setId(1);
        testVo.setName("这个一个测试");
        return Response.success(testVo);
    }

    @GetMapping("/error")
    public Response getError() {
        int x = 9 / 0;
        return Response.success(x);
    }

    @GetMapping("/biz-error")
    public Response getBizError() {
        throw new BizException(ResponseCode.FAIL);
    }

    @PostMapping("/validate-error")
    public Response getValidateError(@RequestBody @Validated TestForm form) {
        return Response.success(form);
    }

    @GetMapping("/assert-error")
    public Response getAssertError() {
        TestForm form = null;
        Assert.notNull(form, "请求参数不存在！");
        return Response.success();
    }

    @GetMapping("/db-get")
    public Response getDbInfo() {
        QueryWrapper<TStd> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<TStd> stdList = this.stdService.getBaseMapper().selectList(queryWrapper);
        return Response.success(stdList);
    }

    @UsedTime
    @GetMapping("/bean-copy")
    public Response beanCopy() {
        QueryWrapper<TStd> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<TStd> stdList = this.stdService.getBaseMapper().selectList(queryWrapper);
        List<CopyVo> copyVoList = this.convertBean.convert(stdList, CopyVo.class);
        return Response.success(copyVoList);
    }
}
